/*
 * File Name：p.java
 * Copyright：Copyright 2008-2015 CiWong.Inc. All Rights Reserved.
 * Description： p.java
 * Modify By：res-dlzhou
 * Modify Date：2015年4月25日
 * Modify Type：Add
 */
package com.meitianhui.convenitenbuy.widget.wheelview;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout.LayoutParams;
import android.widget.PopupWindow;

import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.module.mine.model.Region;
import com.meitianhui.convenitenbuy.widget.wheelview.adapter.ArrayWheelAdapter;

import java.util.List;

/**
 * 此抽象类实现PopWindow滑动选择组件
 *
 * @author res-dlzhou
 * @version ciwong v.1.0 2015年4月25日
 * @since ciwong v.1.0
 */
public abstract class PopWindowWheelSelector implements WheelAdapter,
        OnClickListener {
    /**
     * 显示的PopWindow容器
     */
    public PopupWindow mPopupWindow;

    /**
     * 滚轮视图
     **/
    public WheelView mWheelView;

    /**
     * 市滚轮视图
     **/
    public WheelView mCityWheelView;

    /**
     * 区滚轮视图
     **/
    public WheelView mDistrictWheelView;

    public ArrayWheelAdapter mProvinceAdapter, mCityAdapter, mDistrictAdapter;

//    private T[] items;
//    private T[] itemsCity;
//    private T[] itemsDistrict;

    /**
     * 默认显示的行数
     **/
    private int defult_item_count = 7;

    /**
     * 上下文
     **/
    private Context mContext;

    /**
     * 视图
     **/
    private View contentView;

    /**
     * 取消，确定按钮
     */
    private Button cencelBtn, confirmBtn;

    /**
     * 数据集合
     **/
    public List<Region> list;
    public List<Region.Children> cityList;
    public List<Region.Children.ChildClass> distristList;

    /**
     * 当前选择的Item索引
     **/
    private int curItemIndex, cityCurItemIndex, distrcitCurItemIndex;

    public PopWindowWheelSelector(Context context, List<Region> list, List<Region.Children> cityList, List<Region.Children.ChildClass> distristList) {
        this.mContext = context;
        this.list = list;
        this.cityList = cityList;
        this.distristList = distristList;
        init();
    }

    @Override
    public int getItemsCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public int getMaximumLength() {
        return defult_item_count;
    }

    /**
     * 获取当前选择的项
     *
     * @return
     */
    public Region getCurrentItem() {
        return list.size() != 0 ? list.get(curItemIndex) : null;
    }

    public Region.Children getSCurrentItem() {
        return cityList.size() != 0 ? cityList.get(cityCurItemIndex) : null;
    }

    public Region.Children.ChildClass getOCurrentItem() {
        return distristList.size() != 0 ? distristList.get(distrcitCurItemIndex) : null;
    }

    /**
     * 设置显示的行数
     *
     * @param count
     */
    public void setShowLineCount(int count) {
        this.defult_item_count = count;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_confirm) {
            // 重复选择判断
//            if (curItemIndex != mWheelView.getCurrentItem()) {
//                curItemIndex = mWheelView.getCurrentItem();
//                cityCurItemIndex = mCityWheelView.getCurrentItem();
//                distrcitCurItemIndex = mDistrictWheelView.getCurrentItem();
//                onDismiss(getCurrentItem(), getSCurrentItem(), getOCurrentItem(), curItemIndex);
//            }


            curItemIndex = mWheelView.getCurrentItem();
            cityCurItemIndex = mCityWheelView.getCurrentItem();
            distrcitCurItemIndex = mDistrictWheelView.getCurrentItem();
            onDismiss(getCurrentItem(), getSCurrentItem(), getOCurrentItem(), curItemIndex);
        }
        mPopupWindow.dismiss();
    }

    /**
     * 更新数据
     */
    public void notifyDataSetChanged() {
        curItemIndex = 0;
        mWheelView.notifyDataChange(mProvinceAdapter);
    }

    /**
     * 市更新
     */
    public void cityNotifyDataSetChange() {
        cityCurItemIndex = 0;
        mCityWheelView.notifyDataChange(mCityAdapter);
    }

    /**
     * 区更新
     */
    public void districtNotifyDataSetChange() {
        distrcitCurItemIndex = 0;
        mDistrictWheelView.notifyDataChange(mDistrictAdapter);
    }

    /**
     * 更新数据并设置当前选择的项
     *
     * @param curIndex 要显示的当前项
     */
    public void notifyDataSetChanged(int curIndex) {
        if (curIndex > getItemsCount()) {
            curItemIndex = 0;
        } else {
            curItemIndex = curIndex;
        }
        mWheelView.notifyDataChange(mProvinceAdapter);
    }

    /**
     * 显示
     *
     * @param v
     */
    public void show(View v) {
        this.show(v, curItemIndex);
    }

    /**
     * 显示
     *
     * @param v
     */
    public void show(View v, int curIndex) {
        if (curIndex > getItemsCount()) {
            curItemIndex = 0;
        } else {
            curItemIndex = curIndex;
        }
        mWheelView.setCurrentItem(curItemIndex);
        mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
    }

    /**
     * 消失时回调的结果
     *
     * @param curItem  当前对象
     * @param curIndex 当前对象索引
     */
    public abstract void onDismiss(Region curItem, Region.Children childrenItem, Region.Children.ChildClass childClassItem, int curIndex);

    /**
     * 初始化
     */
    private void init() {
        contentView = LayoutInflater.from(mContext).inflate(
                R.layout.wheels_layout, null);
        mWheelView = (WheelView) contentView.findViewById(R.id.date_wheelView);
        mCityWheelView = (WheelView) contentView.findViewById(R.id.periodTime_wheelView);
        mDistrictWheelView = (WheelView) contentView.findViewById(R.id.time_wheelView);
        cencelBtn = (Button) contentView.findViewById(R.id.btn_cancle);
        confirmBtn = (Button) contentView.findViewById(R.id.btn_confirm);
        mPopupWindow = new PopupWindow(contentView, LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT, true);
        mPopupWindow.setAnimationStyle(R.style.BottomDialogMenuAnim);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.setOutsideTouchable(true);
        cencelBtn.setOnClickListener(this);
        confirmBtn.setOnClickListener(this);
    }


    /**
     * 初始化数据
     *
     * @param mContext
     * @param list
     * @param cityList
     * @param distristList
     */
    public void initWheelAdapter(Context mContext, List<Region> list, List<Region.Children> cityList, List<Region.Children.ChildClass> distristList) {
        mProvinceAdapter = null;
        mProvinceAdapter = new ArrayWheelAdapter(mContext, list.toArray(new Object[list.size()]));
        mCityAdapter = new ArrayWheelAdapter(mContext, cityList.toArray(new Object[cityList.size()]));
        mDistrictAdapter = new ArrayWheelAdapter(mContext, distristList.toArray(new Object[distristList.size()]));


        mWheelView.setCyclic(false);
        mWheelView.setVisibleItems(defult_item_count);
        mWheelView.setCurrentItem(curItemIndex);
        mWheelView.setViewAdapter(mProvinceAdapter);


        mCityWheelView.setCyclic(false);
        mCityWheelView.setVisibleItems(defult_item_count);
        mCityWheelView.setCurrentItem(0);
        mCityWheelView.setViewAdapter(mCityAdapter);


        mDistrictWheelView.setCyclic(false);
        mDistrictWheelView.setVisibleItems(defult_item_count);
        mDistrictWheelView.setCurrentItem(0);
        mDistrictWheelView.setViewAdapter(mDistrictAdapter);

    }

}
